/*
 * @(#)UserInfoFactory.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.framework;

import java.util.Hashtable;
import java.util.List;

import com.dcivision.user.bean.UserGroup;
import com.dcivision.user.dao.UserGroupDAObject;
import com.dcivision.user.dao.UserRecordDAObject;
import com.dcivision.user.dao.UserRoleDAObject;

/**
  UserInfoFactory.java

  This class is for getting user information.

    @author          Rollo Chan
    @company         DCIVision Limited
    @creation date   26/06/2003
    @version         $Revision: 1.11 $
*/

public class UserInfoFactory {

  public static final String REVISION = "$Revision: 1.11 $";

  private static Hashtable hashUserInfo;

  private UserInfoFactory() {
  }

  /**
   * init
   *
   * @param hashIn  Hashtable contains all system parameters
   */
  public static void init(Hashtable hashIn) {
    hashUserInfo = hashIn;
  }

  /**
   * getUserFullName
   *
   * @param key   The user record ID
   * @return      The user full name
   */
  public static String getUserFullName(Integer userRecordID ) {
    if (userRecordID == null) {
      return(null);
    }

    if (hashUserInfo.get(userRecordID) != null) {
      return(((String[])hashUserInfo.get(userRecordID))[0]);
    } else {
      java.sql.Connection conn =null;
      try {
        conn = DataSourceFactory.getConnection();
        UserRecordDAObject userRecordDAO = new UserRecordDAObject(new SessionContainer(), conn);
        String[] userRecord = userRecordDAO.getInactiveObjectByID(userRecordID);
        hashUserInfo.put(userRecordID, userRecord);
        return userRecord[0];
      } catch (Exception e) {
        return null;
      } finally {
        try { conn.close(); } catch (Exception ignore) {} finally { conn = null; }
      }
    }
  }

  /**
   * getUserEmailAddress
   *
   * @param key   The user record ID
   * @return      The user email address
   */
  public static String getUserEmailAddress(Integer userRecordID) {
    if (userRecordID == null) {
      return(null);
    }

    if (hashUserInfo.get(userRecordID) != null) {
      return(((String[])hashUserInfo.get(userRecordID))[1]);
    } else {
      java.sql.Connection conn =null;
      try {
        conn = DataSourceFactory.getConnection();
        UserRecordDAObject userRecordDAO = new UserRecordDAObject(new SessionContainer(), conn);
        String[] userRecord = userRecordDAO.getInactiveObjectByID(userRecordID);
        hashUserInfo.put(userRecordID, userRecord);
        return userRecord[1];
      } catch (Exception e) {
        return null;
      } finally {
        try { conn.close(); } catch (Exception ignore) {} finally { conn = null; }
      }
    }
  }

  /**
   * getUserGroupList
   *
   * @param key   The user record ID
   * @return      The user group list
   */
  public static List getUserGroupList(Integer userRecordID){
      java.sql.Connection conn =null;
      try {
        conn = DataSourceFactory.getConnection();
        UserGroupDAObject userGroupDAO = new UserGroupDAObject(new SessionContainer(), conn);
        List listGroup = userGroupDAO.getListByUserRecordIDGroupType(userRecordID, UserGroup.GROUP_TYPE_PUBLIC);
        return listGroup;
      } catch (Exception e) {
        return null;
      } finally {
        try { conn.close(); } catch (Exception ignore) {} finally { conn = null; }
      }
  }

  /**
   * getUserRoleList
   *
   * @param key   The user record ID
   * @return      The user group list
   */
  public static List getUserRoleList(Integer userRecordID){
      java.sql.Connection conn =null;
      try {
        conn = DataSourceFactory.getConnection();
        UserRoleDAObject userRoleDAO = new UserRoleDAObject(new SessionContainer(), conn);
        List listRole = userRoleDAO.getListByUserRecordID(userRecordID);
        return listRole;
      } catch (Exception e) {
        return null;
      } finally {
        try { conn.close(); } catch (Exception ignore) {} finally { conn = null; }
      }
  }


  /**
   * updateUserInfo
   *
   * @param userRecordID  The user record ID
   * @param userInfo      The user information in String[] format
   */
  public static void updateUserInfo(Integer userRecordID, String[] userInfo) {
    if (userRecordID != null) {
      hashUserInfo.put(userRecordID, userInfo);
    }
  }

}
